package com.ifudata.dvp.app.service.busi.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ifudata.dvp.app.api.app.param.AppMenuRequest;
import com.ifudata.dvp.app.api.app.param.CollectAppMenusRequest;
import com.ifudata.dvp.app.dao.mapper.base.bo.AppMenu;
import com.ifudata.dvp.app.service.atom.interfaces.IAppCollectAtomSV;
import com.ifudata.dvp.app.service.atom.interfaces.IAppMenuAtomSV;
import com.ifudata.dvp.app.service.busi.interfaces.IAppManageBusiSV;
import com.ifudata.dvp.app.util.GetDataUtil;
import com.ifudata.dvp.sdk.util.CollectionUtil;

/**
 * 
 * @description
 * @author libing 2017年9月18日
 * @Modifier
 */
@Service
public class AppManageBusiSVImpl implements IAppManageBusiSV {

	private static final Logger log = LoggerFactory.getLogger(IAppManageBusiSV.class);
	
	@Autowired
	private IAppMenuAtomSV appMenuAtomSV;
	
	@Autowired
	private IAppCollectAtomSV appCollectSV;

	@Override
	public String queryAppMenus(AppMenuRequest request) {
		//BaseListResponse<AppMenuVo> response = new BaseListResponse<AppMenuVo>();
		//List<AppMenuVo> voList = new ArrayList<AppMenuVo>();
		List<AppMenu> menus = appMenuAtomSV.queryAppMenus(request.getParentId());
		
		JSONArray array = new JSONArray(); JSONObject ob;
		for (AppMenu menu : menus) {
			if( menu.getConfigType().equalsIgnoreCase("MENU") ){
				List<List<String>> re = GetDataUtil.getDataBySqlKey(menu);
				if( re.size()!=2) continue;
				if( re.get(0).size()!=re.get(1).size() ) continue;
				ob = new JSONObject();
				ob.put("MENU_ID", menu.getMenuId());
				ob.put("MENU_TITLE", menu.getMenuName());
				for( int i=0; i<re.get(0).size(); i++ ){
					ob.put(re.get(0).get(i), re.get(1).get(i));
				}
				array.add(ob);
			}else if( menu.getConfigType().equalsIgnoreCase("FILE") ){
				ob = new JSONObject();
				ob.put("MENU_ID", menu.getMenuId());
				ob.put("MENU_TITLE", menu.getMenuName());
				ob.put("FILE_PATH", menu.getConfigTitle());
				array.add(ob);
			}
		}
		log.info(array.toJSONString());
		return array.toJSONString();
	}
	
	@Override
	public String queryCollectAppMenus(CollectAppMenusRequest request) {
		List<Long> menuIdList = request.getMenuIdList();
		
		// menuIdList is null 去数据库查 否则认为是更新  为空就是取消所有收藏
		if( menuIdList==null ){
			// 去表里确认一下
			menuIdList = appCollectSV.queryAppCollect(request.getStaffId());
		}else{
			// 更新表
			appCollectSV.coverAppCollect(request.getStaffId(), menuIdList);
		}
		
		JSONArray array = new JSONArray();
		if( CollectionUtil.isEmpty(menuIdList)==false ){
			List<AppMenu> menus = appMenuAtomSV.queryAppMenus(menuIdList);
			
			for (AppMenu menu : menus) {
				List<List<String>> re = GetDataUtil.getDataBySqlKey(menu);
				if( re.size()!=2) continue;
				if( re.get(0).size()!=re.get(1).size() ) continue;
				JSONObject ob = new JSONObject();
				ob.put("MENU_ID", menu.getMenuId());
				ob.put("MENU_TITLE", menu.getMenuName());
				for( int i=0; i<re.get(0).size(); i++ ){
					ob.put(re.get(0).get(i), re.get(1).get(i));
				}
				array.add(ob);
			}
		}
		return array.toJSONString();
	}
	
}
